//
//  CCQSystemAlertView.swift
//  Fangxukanghan
//
//  Created by 常超群的MacBookPro on 2021/6/7.
//

import UIKit

public class CCQSystemAlertView {
    
    /// 多个按钮的alertView     数组里第一个为取消按钮
    /// - Parameters:
    ///   - title: title
    ///   - message: message
    ///   - contentArray: 内容数组
    ///   - controller: 要添加的VC
    ///   - highlightedButtonIndex: 红色选中
    ///   - confirmClosure: 点击回调
    public class func showSystemAlertViewWith(title: String?, message: String?, contentArray: [String], controller: UIViewController?, highlightedButtonIndex: Int? = nil, confirmClosure: ((_ selectedIndex: Int) -> Void)?) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        for (index, _) in contentArray.enumerated() {
            if let highlightedButtonIndex = highlightedButtonIndex, index == highlightedButtonIndex {
                let action = UIAlertAction(title: contentArray[index], style: .destructive) { action in
                    confirmClosure?(index)
                }
                alertController.addAction(action)
            } else {
                let action = UIAlertAction(title: contentArray[index], style: .default) { action in
                    confirmClosure?(index)
                }
                alertController.addAction(action)
            }
        }
        controller?.present(alertController, animated: true, completion: nil)
    }
    /// 多个按钮的ActionSheet     数组里第一个为取消按钮
    /// - Parameters:
    ///   - title: title
    ///   - message: message
    ///   - contentArray: 内容数组
    ///   - controller: 要添加的VC
    ///   - confirmClosure: 点击回调
    public class func showSystemActionSheetWith(title: String?, message: String?, contentArray: [String], controller: UIViewController?, highlightedButtonIndex: Int? = nil, confirmClosure: ((_ selectedIndex: Int) -> Void)?) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
        for (index, _) in contentArray.enumerated() {
            if let highlightedButtonIndex = highlightedButtonIndex, index == highlightedButtonIndex {
                let action = UIAlertAction(title: contentArray[index], style: .destructive) { action in
                    confirmClosure?(index)
                }
                alertController.addAction(action)
            } else {
                let action = UIAlertAction(title: contentArray[index], style: .default) { action in
                    confirmClosure?(index)
                }
                alertController.addAction(action)
            }
        }
        controller?.present(alertController, animated: true, completion: nil)
    }
}
